﻿using System;
using System.Text;
using System.Drawing;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static partial class NativeAOT
{
    [UnmanagedCallersOnly(EntryPoint = "mtcl")]
    public static unsafe double mtcl(double a, double b, IntPtr f_x_ptr)
    {
        f_x = Marshal.GetDelegateForFunctionPointer<delegatefunc_x>(f_x_ptr);

        return mtcl(a, b);
    }

    /// <summary>
    /// Monte_Carlo求积法
    /// f计算被积函数值f(x)的函数名。
    /// </summary>
    /// <param name="a">积分下限。</param>
    /// <param name="b">积分上限。要求b>a。</param>
    /// <returns>函数返回积分值。</returns>
    public static unsafe double mtcl(double a, double b)
    {
        int m;
        double d, x, s;

        s = 0.0;
        d = 65536.0;
        for (m = 0; m <= 65535; m++)
        {
            x = a + (b - a) * rnd.NextDouble();
            s = s + f_x(x);
        }
        s = s * (b - a) / d;
        return (s);
    }

    /*
    // Monte_Carlo求积法例
      int main()
      { 
          double mtclf(double);
          cout <<"s = " <<mtcl(2.5,8.4,mtclf) <<endl;
          return 0;
      }
    // 计算被积函数值
      double mtclf(double x)
      { 
          double y;
          y=x*x+sin(x);
          return(y);
      }
    */
}

